package cn.tedu._04weibo.controller;

import cn.tedu._04weibo.base.response.JsonResult;
import cn.tedu._04weibo.base.response.StatusCode;
import cn.tedu._04weibo.mapper.WeiboMapper;
import cn.tedu._04weibo.pojo.dto.WeiboAddParam;
import cn.tedu._04weibo.pojo.vo.UserVO;
import cn.tedu._04weibo.pojo.vo.WeiboDetailVO;
import cn.tedu._04weibo.pojo.vo.WeiboIndexVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("v1/weibo/")
@Tag(name = "微博管理模块")
public class WeiboController {
    @Autowired
    WeiboMapper weiboMapper;
    @PostMapping("insert")
    @Operation(summary = "01-发布微博")
    @ApiOperationSupport(order = 10)
    public JsonResult insert(@RequestBody WeiboAddParam weiboAddParam, HttpSession session) {
        log.debug("开始处理【01-发布微博】的请求"+weiboAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        if (userVO == null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }else {
            Long id = userVO.getId();
            int rows = weiboMapper.insertWeibo(weiboAddParam,id);
            if (rows > 0) {
                return JsonResult.ok();
            } else {
                return new JsonResult(StatusCode.OPERATION_FAILED);
            }
        }
    }
    @GetMapping("selectIndex")
    @Operation(summary = "02-查询首页微博")
    @ApiOperationSupport(order = 20)
    public JsonResult selectIndex() {
        log.debug("开始处理【02-查询首页微博】的请求");
        List<WeiboIndexVO> list = weiboMapper.selectIndex();
        return JsonResult.ok(list);
    }
    @GetMapping("selectById")
    @Operation(summary = "03-查询微博详情")
    @ApiOperationSupport(order = 30)
    public JsonResult selectById(Integer id) {
        log.debug("开始处理【03-查询微博详情】的请求");
        WeiboDetailVO weiboDetailVO = weiboMapper.selectById(id);
        return JsonResult.ok(weiboDetailVO);
    }

}
